package com.ptszyxx.popose.module.main.mine.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.common.utils.YBusUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.HttpUtils;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.exchange.ExchangeEntity;
import com.ysg.http.data.entity.exchange.ExchangeResult;
import com.ysg.utils.YToastUtil;

import java.util.HashMap;
import java.util.Map;

public class MineExchangeVM extends BaseViewModel<CommonRepository> {
    public ObservableField<ExchangeResult> entity = new ObservableField();

    public MineExchangeVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onRefreshEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onExchangeDialogEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onExchangeRecordCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineExchangeRecord(this));

    //=========================================接口：start===========================================


    @Override
    public void requestList(boolean refresh) {
        if (refresh){
            page = 1;
        }else {
            page ++;
        }

        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.exchangeList(map), this, (v)->{
                    entity.set((ExchangeResult)v.getData());
                    uc.onRefreshEvent.setValue(entity.get().getList());
                });
    }

    public void requestExchange(ExchangeEntity entity) {
        Map<String, String> map = new HashMap<>();
        map.put("id", entity.getId());
        HttpUtils.getInstance()
                .data(model.exchange(map), this, (v)->{
                    YToastUtil.showShort(R.string.success);
                    requestList(true);
                    YBusUtil.exchangeSuccess();
                });
    }

    public void onExchange(ExchangeEntity item){
        uc.onExchangeDialogEvent.setValue(item);
    }
}
